<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
    <script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script src="../../lib/jq-module/jquery.particleground.min.js" charset="utf-8"></script>
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>

<body>
<div class="layui-form layuimini-form" lay-filter="editForm">

    <div class="layui-form-item">
        <label class="layui-form-label required">ID</label>
        <div class="layui-input-block">
            <input type="text" name="id" disabled lay-verify="required" lay-reqtext="ID不能为空" placeholder="请输入ID"
                   value="" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">景点名称</label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="required" lay-reqtext="景点名称不能为空"
                   placeholder="请输入景点名称"
                   value="" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">景点简介</label>
        <div class="layui-input-block">
            <textarea name="introduction" class="layui-textarea" placeholder="请输入备注信息"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">景点图片</label>
        <div class="layui-input-block">
            <button type="button" class="layui-btn" id="test1">
                <i class="layui-icon">&#xe67c;</i>上传图片
            </button>
        </div>
    </div>

    <div class="layui-form-item" style="display:none" hidden>
        <div class="layui-input-block">
            <input type="text" name="imageUrl" id="imageUrl" class="layui-input" style="display:none" hidden>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">发布时间</label>
        <div class="layui-input-block">
            <input type="text" name="createTime" class="layui-input">
        </div>
    </div>


    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        //接参，加载数据
        form.getFormVal = function (rowdata) {
            //可以写其他业务请求。
            var data1 = JSON.parse(rowdata);
            // console.log(data1);
            // 数据回填
            form.val("editForm", data1);
        }

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            data = JSON.parse(JSON.stringify(data.field));
            console.log(data);
            $.ajax({
                type: "post",
                url: "/api/spot?operation=update&id=" + data.id + "&name= " + data.name + " &introduction=" + data.introduction + " &imageUrl=" + data.imageUrl,
                success: function (data) {
                    console.log(data)
                    if (data.code === 0) {
                        layer.msg("更新成功！！");
                    } else {
                        layer.msg("更新失败！！");
                    }
                }
            })
            window.parent.location.reload();
            var iframeIndex = parent.layer.getFrameIndex(window.name);
            parent.layer.close(iframeIndex);
        });

    });
</script>
<script>
    layui.use('upload', function () {
        var upload = layui.upload;
        //执行实例
        var uploadInst = upload.render({
            elem: '#test1' //绑定元素l
            , url: '/api/file/upload' //上传接口
            , done: function (res) {
                layer.msg('上传成功');
                console.log(JSON.parse(JSON.stringify(res)))
                //赋url，等待提交的时候一并提交上去
                $("#imageUrl").val(res.data)
            }
        });
    });
</script>
</body>

</html>